package com.teng.gallerylibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.nineoldandroids.view.ViewHelper;


public class Gallery extends RelativeLayout {

    private ViewPager  viewPager;
    private static int OFFSCREEN_PAGE_LIMIT = 5;
    private QuickPagerAdapter<Object> mQuickPagerAdapter;

    private int galleryPaddingLeft = getResources().getDimensionPixelSize(R.dimen.gallery_left_margin);
    private int galleryPaddingRight = getResources().getDimensionPixelSize(R.dimen.gallery_right_margin);
    private int galleryPaddingTop = getResources().getDimensionPixelSize(R.dimen.gallery_top_margin);
    private int galleryPaddingBottom = getResources().getDimensionPixelSize(R.dimen.gallery_bottom_margin);
    private int galleryCenterMargin = getResources().getDimensionPixelSize(R.dimen.gallery_center_margin);

    public Gallery(Context context) {
        this(context, null);
    }

    public Gallery(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Gallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Gallery);
        Constant.GALLEY_SCALE_RATE = a.getFloat(R.styleable.Gallery_scaleP, Constant.GALLEY_SCALE_RATE);
        int galleryPadding = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding, 0);
        if (galleryPadding > 0) {
            galleryPaddingLeft = galleryPaddingRight = galleryPaddingTop = galleryPaddingBottom = galleryPadding;
        } else {
            galleryPaddingLeft = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_left, galleryPaddingLeft);
            galleryPaddingRight = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_right, galleryPaddingRight);
            galleryPaddingTop = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_top, galleryPaddingTop);
            galleryPaddingBottom = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_bottom, galleryPaddingBottom);
        }
        galleryCenterMargin = a.getDimensionPixelSize(R.styleable.Gallery_gallery_pics_space, galleryCenterMargin);
        a.recycle();
        
        LayoutInflater.from(context).inflate(R.layout.gallery_layout, this, true);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        ((MarginLayoutParams)viewPager.getLayoutParams()).setMargins(galleryPaddingLeft, galleryPaddingTop, galleryPaddingRight, galleryPaddingBottom);
        viewPager.setPageMargin(galleryCenterMargin);
        //viewPager.
        viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return viewPager.dispatchTouchEvent(event);
            }
        });

        viewPager.addOnPageChangeListener(new PageChangeListener());
        viewPager.setPageTransformer(true , new RotateTransformer());
    }
    
    public class PageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {        // arg1 当前页面偏移的百分比
//            updateAnimation(arg0, arg1);
//            invalidate();
            //mQuickPagerAdapter.getLiveView(arg0;
        }

        @Override
        public void onPageSelected(int arg0) {

        }
        
    }

    class RotateTransformer implements ViewPager.PageTransformer {
        // 旋转的最大角度
        private static final float MAX_ROTATE = 5.0f;
        // 旋转过程中的角度
        private float currentRotate;
        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            if (position < -1) {
                ViewHelper.setPivotX(view, pageWidth);
                ViewHelper.setPivotY(view, view.getHeight());
                ViewHelper.setRotation(view, -MAX_ROTATE);
            } else if (position <= 0) {
                // position范围[-1.0,0.0],此时A页动画移出屏幕
                currentRotate = position * MAX_ROTATE;
                // 设置当前页的旋转中心点，横坐标是屏幕宽度,纵坐标为屏幕的高度
                ViewHelper.setPivotX(view, pageWidth);
                ViewHelper.setPivotY(view, view.getHeight());
                ViewHelper.setRotation(view, currentRotate);
            } else if (position <= 1) {
                // position范围(0.0,1.0],此时B页动画移到屏幕
                currentRotate = position * MAX_ROTATE;
                // 设置当前页的旋转中心点，横坐标是0,纵坐标为屏幕的高度
                ViewHelper.setPivotX(view, 0);
                ViewHelper.setPivotY(view, view.getHeight());
                ViewHelper.setRotation(view, currentRotate);
            } else {
                ViewHelper.setPivotX(view, 0);
                ViewHelper.setPivotY(view, view.getHeight());
                ViewHelper.setRotation(view, MAX_ROTATE);
            }
        }
    }

    /**
     * 更新pager变动动画
     * @param leftPostion 左侧pager的position
     * @param seed 变动因子，当前页面偏移的百分比
     */
    public void updateAnimation(int leftPostion, float seed) {
//a++;
//        VersionDiffUtils.rotate(mQuickPagerAdapter.getLiveView(leftPostion) , (int) (a+1));

        if (seed < 0.5) {

            float f = Constant.GALLEY_SCALE_RATE + (0.5f - seed) / 0.5f * (1f - Constant.GALLEY_SCALE_RATE);
//            VersionDiffUtils.scaleY(mQuickPagerAdapter.getLiveView(leftPostion),
//                    f);
//            VersionDiffUtils.scaleX(mQuickPagerAdapter.getLiveView(leftPostion),
//                    f);

//            Log.e("hhhhh" , "小于   " + f +" --- "+seed);
            transformPage(mQuickPagerAdapter.getLiveView(leftPostion) , f);

        } else {
            float f = Constant.GALLEY_SCALE_RATE + (seed - 0.5f) / 0.5f * (1f - Constant.GALLEY_SCALE_RATE);
            transformPaget(mQuickPagerAdapter.getLiveView(leftPostion + 1), f);
//            VersionDiffUtils.scaleY(mQuickPagerAdapter.getLiveView(leftPostion+1),
//                    f);
//            VersionDiffUtils.scaleX(mQuickPagerAdapter.getLiveView(leftPostion + 1),
//                    f);
//            Log.e("hhhhh", "大于  " + f+" --- "+seed);
        }

    }

    private static final float MAX_ROATE = 20f;

    public void transformPaget(View view, float position) {
        int width = view.getWidth();
        int pageWidth = width;
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)
            ViewHelper.setRotation(view, 0);
        } else if (position <= 1) { // [-1,1]
            float result=position*MAX_ROATE;
            ViewHelper.setPivotX(view,-pageWidth*0.5f);
            ViewHelper.setPivotY(view,pageHeight);
            ViewHelper.setRotation(view,result);
        } else { // (1,+Infinity]
            ViewHelper.setRotation(view,0);
        }
    }

    public void transformPage(View view, float position) {
        int width = view.getWidth();
        int pageWidth = width;
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)
            ViewHelper.setRotation(view, 0);
        } else if (position <= 1) { // [-1,1]
            float result=position*MAX_ROATE;
            ViewHelper.setPivotX(view,pageWidth*0.5f);
            ViewHelper.setPivotY(view,pageHeight);
            ViewHelper.setRotation(view,result);
        } else { // (1,+Infinity]
            ViewHelper.setRotation(view,0);
        }
    }

    public ViewPager getViewPager() {
        return viewPager;
    }


//    public ChildViewPager getViewPager() {
//        return viewPager;
//    }

    public void setAdapter(QuickPagerAdapter adapter) {
        viewPager.setAdapter(adapter);
        mQuickPagerAdapter = adapter; 
    }
  
}
